package com.atguigu.mybatisplus;

import com.atguigu.mybatisplus.entity.User;
import com.atguigu.mybatisplus.mapper.UserMapper;
import com.atguigu.mybatisplus.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author MI
 * @ClassName LambdaWrapper.java
 * @createTime 2021年08月19日 17:15:00
 */
@SpringBootTest
public class LambdaWrapper {
    @Resource
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @Test
    public void updateLambda() {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<User>();
        updateWrapper.eq(User::getId, 1428281397106294786L).eq(User::getAge, 28);
        updateWrapper.set(User::getName, "updateWrapper");
        updateWrapper.set(User::getUpdateTime, LocalDateTime.now());
        userMapper.update(null, updateWrapper);
    }

    /**
     * 测试 setsql
     */
    @Test
    public void updateLambdaSetSql() {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<User>();
        updateWrapper.eq(User::getId, 1L);
        //如果不添加 单引号，会作为sql字段处理 |生成的SQL UPDATE user SET name='updateWrapper',age=age+1,create_time='2022-03-17T10:12:02.688207600' WHERE (id = ?)
        updateWrapper.setSql(true, "name=" + "'updateWrapper'");
        updateWrapper.setSql(true, "age=age+1");
        updateWrapper.setSql("create_time='" + LocalDateTime.now().plusDays(1) + "'");
        userMapper.update(null, updateWrapper);
    }

    /**
     * TODO 错误的例子|根据 whereWrapper 条件，更新记录(条件在 wrapper里面)
     * <code>UPDATE user SET name=?, update_time=?, name=?,update_time=?</code>
     */
    @Test
    public void updateLambdaEntity() {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<User>();
        User user = new User();
        user.setId(1428281397106294786L);
        user.setName("错误率");
        updateWrapper.set(User::getName, "糟糕");
        updateWrapper.set(User::getUpdateTime, LocalDateTime.now());
        userMapper.update(user, updateWrapper);
    }

    /**
     * 测试链式操作
     */
    @Test
    public void chainOper() {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(User::getId);
        List<User> list = userService.list(queryWrapper);
        list.forEach(System.out::println);

        // 操作后每个集合都会改变
        list.stream().forEach(vo -> {
            // vo.setUpdateTime(LocalDateTime.now());
        });
        System.out.println(StringUtils.repeat("*", "-", 30));
        list.forEach(System.out::println);
    }

    @Test
    public void testIn() {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(User::getId);

        queryWrapper.in(User::getId, list);
        List<User> userList = userService.list(queryWrapper);
        userList.forEach(System.out::println);
    }

    @Test
    public void queryVo() {
        //this.list(Wrappers.<SysUser>query().lambda().eq(SysUser::getDeptId, parentId));
        userService.getOne(Wrappers.<User>query().lambda().eq(User::getId, 1L));
    }
}
